﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace $RootNamespace$

	Friend Class $Request$
        Private _data As Object

        Public ReadOnly Property Target() As Integer
            Get
                Return CInt(Fix(_data))
            End Get
        End Property
        Friend Sub New(ByVal data As Object)
            _data = data
        End Sub
    End Class
    ' "$Handler$" 
	Friend MustInherit Class $Handler$
        ' private $Handler$ successor;

		Private privateSuccessor As $Handler$
		Public Property Successor() As $Handler$
            Get
                Return privateSuccessor
            End Get
			Set(ByVal value As $Handler$)
                privateSuccessor = value
            End Set
        End Property
        Protected MustOverride Function CanHandle(ByVal request As $Request$) As Boolean
        Friend Shared Sub Test()
            ' Setup Chain of Responsibility 
			Dim h1 As $Handler$ = New $ConcreteHandlerA$()
			Dim h2 As $Handler$ = New $ConcreteHandlerB$()
			Dim h3 As $Handler$ = New $ConcreteHandlerC$()
            h1.Successor = h2
            h2.Successor = h3
            'Generate  requests
            Dim requests() As $Request$ = {New $Request$(1), New $Request$(7), New $Request$(15), New $Request$(23), New $Request$(13), New $Request$(6), New $Request$(28), New $Request$(23)}
            'process requests
            For Each request As $Request$ In requests
                h1.HandleRequest(request)
            Next request
        End Sub

        Public MustOverride Sub HandleRequest(ByVal request As $Request$)
    End Class

    ' "$ConcreteHandlerA$" 
    Friend Class $ConcreteHandlerA$
        Inherits $Handler$
        Protected Overrides Function CanHandle(ByVal request As $Request$) As Boolean
            Return request.Target >= 0 AndAlso request.Target < 10
        End Function
        Public Overrides Sub HandleRequest(ByVal request As $Request$)
            If CanHandle(request) Then
                Console.WriteLine("{0} handled request {1}", Me.GetType().Name, request.Target)

            ElseIf Successor IsNot Nothing Then
                Successor.HandleRequest(request)
            End If
        End Sub
    End Class

    ' "$ConcreteHandlerB$" 
    Friend Class $ConcreteHandlerB$
        Inherits $Handler$
        Protected Overrides Function CanHandle(ByVal request As $Request$) As Boolean
            Return request.Target >= 10 AndAlso request.Target < 20
        End Function
        Public Overrides Sub HandleRequest(ByVal request As $Request$)
            If CanHandle(request) Then
                Console.WriteLine("{0} handled request {1}", Me.GetType().Name, request.Target)

            ElseIf Successor IsNot Nothing Then
                Successor.HandleRequest(request)
            End If
        End Sub
    End Class

    ' "$ConcreteHandlerC$" 
	Friend Class $ConcreteHandlerC$
		Inherits $Handler$
        Protected Overrides Function CanHandle(ByVal request As $Request$) As Boolean
            Return request.Target >= 20 AndAlso request.Target < 30
        End Function
        Public Overrides Sub HandleRequest(ByVal request As $Request$)
            If CanHandle(request) Then
                Console.WriteLine("{0} handled request {1}", Me.GetType().Name, request.Target)

            ElseIf Successor IsNot Nothing Then
                Successor.HandleRequest(request)
            End If
        End Sub
    End Class
End Namespace
